/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.ams.controller;

import cn.tedu.mall.ams.service.IPermissionService;
import cn.tedu.mall.common.pojo.admin.dto.PermissionAddDTO;
import cn.tedu.mall.common.pojo.admin.dto.PermissionUpdateDTO;
import cn.tedu.mall.common.pojo.admin.query.PermissionQuery;
import cn.tedu.mall.common.pojo.admin.vo.PermissionVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/ams/permission")
@Api(tags = "权限模块")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    /**
     * 新增权限
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "新增权限")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/save")
    public JsonResult addPermission(PermissionAddDTO permissionAddDTO) {
        permissionService.addPermission(permissionAddDTO);
        return JsonResult.ok();
    }

    /**
     * 权限查询搜索
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "后台权限搜索列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<PermissionVO>> listPermissions(PermissionQuery permissionQuery) {
        JsonPage jsonPage = permissionService.listPermissions(permissionQuery);
        return JsonResult.ok(jsonPage);
    }

    /**
     * 编辑权限，但是不可轻易动路径
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "编辑权限", notes = "保留接口，但是权限不可轻易编辑")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult updatePermission(PermissionUpdateDTO permissionUpdateDTO) {
        permissionService.updatePermission(permissionUpdateDTO);
        return JsonResult.ok();
    }

    /**
     * 删除权限
     */
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "删除权限", notes = "保留接口，但是权限不可轻易编辑")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult deletePermission(Long id) {
        permissionService.deletePermission(id);
        return JsonResult.ok();
    }
}
